<template>
	<view>
		<unicloud-db v-slot:default="{data, loading, error, options}" collection="class-list">
			<view v-if="error">{{error.message}}</view>
			<view v-else>
				<uni-card>
					<!-- 班级人数 -->
					<p>{{class_user_account}}人</p>
					<!-- 班级名称 -->
					<p>{{class_name}}</p>

				</uni-card>
			</view>
		</unicloud-db>
		
	</view>
</template>

<script>
	import {
		store,
		mutations
	} from '@/uni_modules/uni-id-pages/common/store.js'
	export default {
		data() {
			return {
				class_name:"",
				class_user_account:0
			}
		},
		methods: {
			homworkDetail(item){
				console.log('item',item.class_name);
				let class_name = item.class_name
				const db = uniCloud.database()
				// 使用`jql`查询list表内`name`字段值为`hello-uni-app`的记录
				db.collection('class-list')
				// "user_id =='"+current_user_id+"'"
				  .where("class_name =='"+class_name+"'")
				  .field('_id')
				  .get()
				  .then((res)=>{
				    // 通过name查找id
					let user_id = res.result['data'][0]['_id']
					this.user_id=user_id
					uni.reLaunch({
						url: `/pages/list/homework/homework?user_id=${user_id}`
					})
				  }).catch((err)=>{
				    // err.message 错误信息
				    // err.code 错误码
				  })

				
			}
		},
		onLoad() {
			console.log('this.userInfo',this.userInfo);
			const db = uniCloud.database()
			db.collection('class-list')
			.get()
			.then((res)=>{
				let class_list_info = res.result['data']
				for (let i=0;i<class_list_info.length;i++) {
					console.log('class_list_info',class_list_info[i]);
					let user_id_info = class_list_info[i].user_id
					console.log('user_id_info',user_id_info);
					// 如果用户id在班级存储的id里面
					if(user_id_info.includes(this.userInfo['_id'])){
						console.log('this.userInfo',this.userInfo['_id']);
						this.class_name = class_list_info[i].class_name
						this.class_user_account  = class_list_info[i].user_id.length
					}
				}
			})
		},
		computed:{
			// 获取当前用户信息
			userInfo() {
				return store.userInfo
			}
			
		}
	}
</script>

<style>

</style>
